<template>
	<view class="zy-column-full">
		<zy-title rightText="保存" @right="requestSave"></zy-title>
		
		<scroll-view class="zy-scroll zy-ph-lg" scroll-y>
			<view class="zy-text-white-lg-bold zy-mt-lg" style="font-size: 42rpx;">编辑个人资料</view>
			<!-- 基本信息 -->
			<view class="zy-text-whitegrey-lg-bold zy-mt-lg">基本信息</view>
			<zy-cell-item label="头像" @click.native="chooseAvatar">
				<image class="zy-img-sm-circle" :src="user.headPicUrl"></image>
			</zy-cell-item>
			<zy-cell-item label="昵称" :content="user.nick" @click.native="toUserEditContent(1)"></zy-cell-item>
			<zy-cell-item label="性别" :content="user.sex == 2? '女': '男'" :arrow="false"></zy-cell-item>
			<zy-cell-item label="添加微信" :content="user.weChat || '请填写'" @click.native="toUserEditContent(2)"></zy-cell-item>
			<zy-cell-item label="生日">
				<picker mode="date" fields="day" :start="startDate" :end="endDate" @change="birthdayConfirm">
					<view class="zy-text-white-lg-bold">{{user.birthday || '请选择生日'}}</view>
				</picker>
			</zy-cell-item>
			<zy-cell-item label="个性签名" :content="user.sign || '请填写'" @click.native="toUserEditContent(3)"></zy-cell-item>
			<!-- 详细信息 -->
			<view class="zy-text-whitegrey-lg-bold zy-mt-100">详细信息</view>
			<zy-cell-item label="身高">
				<picker :value="heightIndex" :range="heightList" range-key="height"
					@change="heightConfirm">
					<view class="zy-text-white-lg-bold">{{user.heightStr || '请选择身高'}}</view>
				</picker>
			</zy-cell-item>
			<zy-cell-item label="体重">
				<picker :value="weightIndex" :range="weightList" range-key="weight"
					@change="weightConfirm">
					<view class="zy-text-white-lg-bold">{{user.weightStr || '请选择体重'}}</view>
				</picker>
			</zy-cell-item>
			<!-- 我的标签 -->
			<view class="zy-text-whitegrey-lg-bold zy-mt-100">我的标签</view>
			<zy-tag class="zy-row-wrap zy-mt" add :list="tags" @add="toEditTag()"></zy-tag>
		</scroll-view>
	</view>
</template>

<script>
	import { uploadOSS } from '@/js_sdk/jason-alioss-upload/oss.js'
	import constant from '@/common/constant.js';
	import CacheUtil from '@/main/common/CacheUtil.js';
	import RouteUtil from '@/main/common/RouteUtil.js';
	import TimeUtil from '@/main/common/TimeUtil.js';

	export default {
		data() {
			return {
				user: {},
				photos: [],
				introPicIds: '',
				sourceType: ['album', 'camera'],
				currentPicIndex: 0,
				editType: '', //1.昵称；2.个性签名
				editTitle: '',
				editContent: '',
				nick: '',
				tags: [],
				heightIndex: 0,
				heightList: [],
				weightIndex: 0,
				weightList: [],
				incomeIndex: 0,
				incomeList: [],
				educationIndex: 0,
				educationList: [],
				workerindex: [0, 0],
				workerList: [],
				subWorkerList: [],
			}
		},
		computed: {
			startDate() {
				return TimeUtil.getYearAgo(60);
			},
			endDate() {
				return TimeUtil.getYearAgo(20);
			}
		},
		onLoad(option) {
			this.requestUser();
			this.requestDict();
			uni.$on('sendTags', res => {
				this.tags = res.list;
				this.user.tagIds = res.tagIds;
			})
			uni.$on('sendContent', res => {
				if(1 == res.type){
					this.user.nick = res.content;
				}else if(2 == res.type){
					this.user.weChat = res.content;
				}else if(3 == res.type){
					this.user.sign = res.content;
				}
			})
		},
		onUnload(){
			uni.$off('sendTags')
			uni.$off('sendContent')
		},
		methods: {
			toUserEditContent(type){
				let content = this.user.nick;
				if(2 == type){
					content = this.user.weChat || '';
				}else if(3 == type){
					content = this.user.sign;
				}
				RouteUtil.toUserEditContent({type:type, content: content})
			},
			chooseAvatar() {
				const carmeraAvatar = CacheUtil.getCarmeraEdit();
				if (carmeraAvatar) {
					this.uploadOOS()
					return
				}
				uni.showModal({
					content: '拍照功能用于上传头像',
					success: res => {
						if (res.confirm) {
							CacheUtil.setCarmeraEdit()
							this.uploadOOS()
						}
					}
				})
			},
			uploadOOS() {
				uni.chooseImage({
					count: 1,
					crop: {
						width: 400,
						height: 400
					},
					success: (file) => {
						let dir = 'userinfo'
						uploadOSS(dir, file.tempFilePaths[0]).then(res => {
							this.requestFile(dir, res.fileName, res.picUrl)
						}).catch(err=>{
						})
					}
				});
			},
			requestFile(pathKey, fileNameKey, picUrl) {
				let params = {
					pathKey: pathKey,
					userIdKey: this.user.id,
					fileNameKey: fileNameKey
				}
				uni.$http.post('/api/ossSts/callBackFiInfo', params).then(res=>{
					this.requestAvatar(res.data, picUrl)
				})
			},
			// 新增图片
			selectPhoto(event) {
				this.introPicIds = ''
				this.currentPicIndex = 0
				event.tempFilePaths.forEach((file, index) => {
					let dir = 'userinfo'
					uploadOSS(dir, file).then(res => {
						this.requestFilePhoto(dir, res.fileName, res.picUrl, index, event.tempFilePaths.length)
					}).catch(err=>{
						this.currentPicIndex++;
					})
				});
			},
			requestFilePhoto(pathKey, fileNameKey, picUrl, index, length) {
				let params = {
					pathKey: pathKey,
					userIdKey: this.user.id,
					fileNameKey: fileNameKey
				}
				uni.$http.post('/api/ossSts/callBackFiInfo', params).then(res=>{
					this.currentPicIndex++;
					this.photos.push({
						url: picUrl,
						id: res.data
					});
					if (this.introPicIds) {
						this.introPicIds += ',' + res.data;
					} else {
						this.introPicIds = res.data;
					}
					if (this.currentPicIndex == length) {
						this.requestUploadPhoto(1)
					}
				}).catch(res=>{
					this.currentPicIndex++;
				})
			},
			requestUploadPhoto(useFileType) {
				let params = {
					useFileType: useFileType,
					introPicIds: this.introPicIds,
					id: this.user.id
				}
				uni.$http.updateUser(params)
			},
			deletePhoto(event) {
				this.introPicIds = ''
				this.photos = this.photos.filter(item => {
					if (item.url === event.tempFilePath) {
						this.introPicIds = item.id
					}
					return item.url !== event.tempFilePath;
				})
				this.requestUploadPhoto(2)
			},
			birthdayConfirm(event) {
				let birthday = event.detail.value;
				uni.$http.updateUser({
					birthday: birthday,
				}).then(res => {
					this.user.birthday = birthday;
				})
			},
			heightConfirm(event) {
				const data = this.heightList[event.detail.value];
				uni.$http.updateUser({
					heightId: data.id,
				}).then(res => {
					this.user.heightStr = data.height;
				})
			},
			weightConfirm(event) {
				const data = this.weightList[event.detail.value];
				uni.$http.updateUser({
					weightId: data.id,
				}).then(res => {
					this.user.weightStr = data.weight;
				})
			},
			incomeConfirm(event) {
				const data = this.incomeList[event.detail.value];
				uni.$http.updateUser({
					incomeId: data.id,
				}).then(res => {
					this.user.incomeStr = data.name;
				})
			},
			educationConfirm(event) {
				const data =this.educationList[event.detail.value];
				uni.$http.updateUser({
					educId: data.id,
				}).then(res => {
					this.user.educStr = data.name;
				})
			},
			workerChange(event) {
				this.workerindex[event.detail.column] = event.detail.value;
				if (event.detail.column == 0) {
					this.workerList[1] = this.subWorkerList[event.detail.value];
					this.workerindex[1] = 0;
				}
				this.$forceUpdate();
			},
			workerConfirm(event) {
				const data = this.workerList[1][event.detail.value[1]];
				uni.$http.updateUser({
					careerId: data.id,
				}).then(res => {
					this.user.careerStr = data.name;
				})
			},

			toEditTag() {
				RouteUtil.toUserEditTag(this.user)
			},

			requestUser() {
				uni.$http.getUser().then(res => {
					this.user = res.data;
					//形象照
					if (this.user.introPicIdUrl) {
						let ids = this.user.introPicIds.split(',')
						this.user.introPicIdUrl.forEach((item, index) => {
							this.photos.push({
								url: item,
								id: ids[index]
							});
						});
					}
					if (this.user.tagIdList) {
						this.user.tagIdList.forEach(item=>{
							this.tags.push({name:item});
						})
					}
				})
			},
			requestDict() {
				uni.$http.get('/api/dict/detail').then(res => {
					this.heightList = res.data.dictHeights
					this.heightList.forEach(item=>{
						item.height += item.unit;
					})
					this.weightList = res.data.dictWeights
					this.weightList.forEach(item=>{
						item.weight += item.unit;
					})
					this.incomeList = res.data.dictIncomes
					this.incomeList.forEach(item=>{
						item.name += item.unit;
					})
					this.educationList = res.data.dictEducs
					let workerOne = [];
					res.data.dictCareerNodeVos.forEach(item => {
						workerOne.push(item);
						this.subWorkerList.push(item.children);
					});
					this.workerList.push(workerOne);
					this.workerList.push(this.subWorkerList[0]);
				})
			},
			requestAvatar(picId, picUrl) {
				uni.$http.updateUser({
					headPicId: picId
				}).then(res => {
					this.user.headPicUrl = picUrl;
					CacheUtil.setUserPic(picUrl)
				})
			},
			requestSave(){
				uni.$toast.showToast('保存成功')
				RouteUtil.back()
			},
		}
	}
</script>

<style lang="scss" scoped>
	
</style>